//
//  SSRequest.m
//  SSNetworkDemo
//
//  Created by su on 16/6/15.
//  Copyright © 2016年 SS. All rights reserved.
//

#import "SSRequest.h"
#import "AFNetworking.h"

#pragma mark - requestConfig
@interface SSRequestConfig : NSObject

@property(nonatomic, assign) SSNetworkStatus networkStatus;
@property(nonatomic, assign) BOOL logSwitch;
@property(nonatomic, strong) NSString *basePath;
@property(nonatomic, strong) NSMutableDictionary *headers;

@property(nonatomic, strong) AFHTTPSessionManager *sessionManager;

+ (SSRequestConfig *) shared;

- (void) MonitorNetWorkStatus;

@end
@implementation SSRequestConfig

+ (SSRequestConfig *) shared
{
    static SSRequestConfig *config;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        config = [[SSRequestConfig alloc] init];
        
    });
    return config;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.logSwitch = YES;
        self.headers = [[NSMutableDictionary alloc] init];
        self.networkStatus = SSNetworkStatusReachableWiFi;
        
        //AFNetworking config
        self.sessionManager = [AFHTTPSessionManager manager];
    }
    return self;
}


- (void) MonitorNetWorkStatus
{
    self.networkStatus = (NSInteger)[[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus];
    __weak __typeof(self) weakSelf = self;
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        __strong __typeof(self) strongSelf = weakSelf;
        strongSelf.networkStatus = (NSInteger)status;
    }];
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

@end




#pragma mark - request
@interface SSRequest ()

@property(nonatomic, strong) NSString *url;
@property(nonatomic, strong) NSDictionary *params;

- (id) initWithUrl:(NSString *)url parameters:(NSDictionary *)parameters;

@end

@implementation SSRequest

+ (void) MonitorNetWorkStatus
{
    
}

+ (void) SetDebugLoggerSwitch:(BOOL)on
{
    [[SSRequestConfig shared] setLogSwitch:on];
}

+ (void) SetApiBasePath:(NSString *)basePath
{
    [[SSRequestConfig shared] setBasePath:basePath];
}

+ (void) SetHTTPHeader:(NSString *)header value:(NSString *)value
{
    [[[SSRequestConfig shared] headers] setObject:value forKey:header];
}

+ (SSRequest *) requestWithUrl:(NSString *)url
{
    return [[SSRequest alloc] initWithUrl:url parameters:nil];
}
+ (SSRequest *) requestWithUrl:(NSString *)url parameters:(NSDictionary *)parameters
{
    return [[SSRequest alloc] initWithUrl:url parameters:parameters];
}

- (id) initWithUrl:(NSString *)url parameters:(NSDictionary *)parameters method:(SSRequestMethod)method
{
    self = [super init];
    if (self) {
        self.url = url;
        self.params = parameters;
        self.method = method;
    }
    return self;
}

- (void) send
{
    
}

- (void) sendWithCallback:(SSRequestCallback)callback
{
    
}


@end
